<script setup lang='ts'>
import { onMounted, ref } from "vue"
import { useRouter } from 'vue-router'
import { getSearchHotWordsInfo, getSearchSuggest } from "@/api/index"
import { showConfirmDialog } from 'vant';
import { showFailToast } from 'vant';
import 'vant/es/dialog/style';
const $router = useRouter()
const search = ref<any>()  //input对象

const hotWords = ref<any>()
onMounted(async () => {
    search.value.focus()
    let hotWordsRes = await getSearchHotWordsInfo()
    hotWords.value = hotWordsRes.data.list

    localHistoryList.value = JSON.parse(`${localStorage?.getItem('wangyiyun-search')}`) || [];

})
const toSearch = (book: any) => {

    localHistoryList.value = new Set([book.name || book.text, ...localHistoryList.value])
    localStorage.setItem('wangyiyun-search', JSON.stringify([...new Set([book.name || book.text, ...localHistoryList.value])]))

    $router.push({ path: '/detail', query: { id: book.id, title: book.name || book.text } })
}

const searchtext = ref<string>('')
const searchSuggest = ref<any>()
const time = ref<any>(null)
const searchInput = () => {

    if (time.value != null) {
        clearTimeout(time.value)
    }
    time.value = setTimeout(async () => {
        if (searchtext.value != '') {
            let res = await getSearchSuggest(searchtext.value)
            searchSuggest.value = res.data.words.w
        }
        clearTimeout(time.value)
    }, 1000)
}

const localHistoryList = ref<any>([])
const inputToSearch = () => {
    localHistoryList.value = new Set([searchtext.value, ...localHistoryList?.value])
    localStorage.setItem('wangyiyun-search', JSON.stringify([...new Set([searchtext.value, ...localHistoryList.value])]))
    $router.push({ path: '/search-show', query: { text: searchtext.value } })
}

const historyToSearch = (word: string) => {
    searchtext.value = word;
    $router.push({ path: '/search-show', query: { text: searchtext.value } })
}

const delHistory = () => {
    showConfirmDialog({
        message:
            '是否清空最近搜索？',
    })
        .then(() => {
            localHistoryList.value = []
            localStorage.setItem('wangyiyun-search', JSON.stringify([]))
        })
        .catch(() => {
            showFailToast('删除失败!');
        });
}
</script>

<template>
    <div class="search">
        <div class="search-title">
            <div class="left">
                <img src="@/assets/imgs/搜索1.png" alt="">
                <input type="text" placeholder="唐诗里的烟火人间" ref="search" v-model="searchtext" @input="searchInput"
                    @keyup.enter="inputToSearch">
                <img src="@/assets/imgs/叉叉1.png" alt="" v-if="searchtext != ''" @click="searchtext = ''">
            </div>
            <span @click="$router.back()">取消</span>
        </div>
        <div class="search-hot">
            <span>热门搜索</span>
            <ul class="list">
                <li v-for="w, index in hotWords" :key="w.id" @click="toSearch(w)">
                    <span class="first" :class="index + 1 == 1 ? 'first' : (index + 1 == 2 ? 'second' : 'third')"
                        v-if="index <= 2">{{
                        index+ 1 }}</span>
                    <span>{{ w.name }}</span>
                </li>
            </ul>
        </div>
        <div class="search-history">
            <div class="history-title">
                <span>最近搜索</span>
                <img src="@/assets/imgs/删除1.png" alt="" @click="delHistory">
            </div>
            <ul class="history-list">
                <li v-for="word, index in localHistoryList" :key="index" @click="historyToSearch(word)">
                    <img src="@/assets/imgs/搜索1.png" alt="">
                    <span>{{ word }}</span>
                </li>
            </ul>
        </div>
        <div class="search-content" v-if="searchtext != ''">
            <div class="content" @click="$router.push({ path: '/search-show', query: { text: searchtext } })">
                <img src="@/assets/imgs/搜索1.png" alt="">
                <p>搜索"<span>{{ searchtext }}</span>"</p>
                <img src="@/assets/imgs/右边1.png" alt="">
            </div>
            <ul class="list">
                <li v-for="s, index in searchSuggest" :key="index" @click="toSearch(s)">
                    <img src="@/assets/imgs/书1.png" alt="">
                    <span>{{ s.text }}</span>
                    <img src="@/assets/imgs/右边1.png" alt="">
                </li>
            </ul>
        </div>
    </div>
</template>

<style lang="less" scoped>
.search {
    width: 100vw;
    height: 100vh;
    background-color: #eee;
    position: relative;

    .search-title {
        background-color: #fff;
        width: 100%;
        height: 50rem;
        padding: 0 10rem;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .left {
            display: flex;
            align-items: center;
            overflow: hidden;
            width: 85%;
            height: 30rem;
            background-color: #eee;
            border-radius: 20rem;

            img {
                width: 20rem;
                height: 20rem;
                margin-left: 5rem;

                &:last-child {
                    margin-right: 8rem;
                }
            }

            input {
                flex-grow: 1;
                border: none;
                background-color: #eee;
            }
        }

        >span {
            font-size: 16rem;
            color: #888;
        }
    }

    .search-hot {
        padding: 30rem 10rem 0;
        width: 100%;

        span {
            font-size: 14rem;
            color: #666;
        }

        ul.list {
            padding: 0 15rem;
            margin-top: 10rem;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;

            li {
                width: 50%;
                display: flex;
                align-items: center;
                margin-bottom: 20rem;

                span {
                    color: #2d2d2d;

                    &.first {
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        width: 20rem;
                        height: 20rem;
                        color: #fff;
                        padding: 2rem;
                        border-radius: 50%;
                        margin-right: 10rem;
                    }

                    &.first {
                        background-color: #fc1c1c;
                    }

                    &.second {
                        background-color: salmon;
                    }

                    &.third {
                        background-color: orange;
                    }
                }
            }
        }
    }

    .search-history {
        padding: 30rem 10rem;
        width: 100%;


        .history-title {
            display: flex;
            justify-content: space-between;
            align-items: center;

            img {
                width: 25rem;
            }

            span {
                font-size: 14rem;
                color: #666;
            }
        }

        ul.history-list {
            display: flex;
            flex-wrap: wrap;
            padding: 10rem 0;

            li {
                display: flex;
                align-items: center;
                padding: 3rem 10rem;
                // border: 1rem solid #ccc;
                background-color: rgb(228, 225, 225);
                border-radius: 20rem;
                margin-right: 10rem;
                margin-bottom: 10rem;

                img {
                    width: 15rem;
                }

                span {
                    color: #2d2d2d;
                    font-size: 14rem;
                }
            }
        }

    }

    .search-content {
        width: 100vw;
        height: calc(100vh - 50rem);
        position: absolute;
        top: 50rem;
        left: 0;
        background-color: rgb(255, 251, 251);
        padding: 0 10rem;

        .content {
            display: flex;
            padding: 10rem 0;
            align-items: center;
            border-bottom: 1rem solid #eee;

            img {
                width: 20rem;
                height: 20rem;
                justify-content: left;
            }

            p {
                flex-grow: 1;
                margin-left: 10rem;
                color: #2d2d2d;
                font-size: 16rem;

                span {
                    font-size: 16rem;
                    color: skyblue
                }
            }
        }

        ul.list {
            li {
                display: flex;
                padding: 10rem 0;
                border-bottom: 1px solid #eee;

                img {
                    width: 20px;
                }

                span {
                    font-size: 16rem;
                    color: #2d2d2d;
                    margin-left: 10rem;
                    flex-grow: 1;
                }
            }
        }
    }

}
</style>
